﻿using System;
using System.Collections.Generic;
using System.IO;

namespace FunctionalCLR.Core {
    public class DefaultConstantManager : IConstantManager {

        private const string CONST_INFI = "infinity";

        private Dictionary<string, string> constants;

        public DefaultConstantManager(string fileName) {
            this.constants = new Dictionary<string, string>();
            FileStream fs = new FileStream(fileName, FileMode.Open);
            StreamReader reader = new StreamReader(fs);

            string ln = string.Empty;
            while ((ln = reader.ReadLine()) != null) {
                string name = ln.Split('=')[0];
                string value = ln.Split('=')[1];

                constants[name] = value;
            }

            reader.Close();
        }
        #region IConstantManager Members

        public bool IsConstant(string name) {
            if (name == CONST_INFI) {
                return true;
            }

            return this.constants.ContainsKey(name);
        }

        public string GetConstant(string name) {
            if (name == CONST_INFI) {
                return Int32.MaxValue.ToString();
            }

            return this.constants[name];
        }

        #endregion
    }
}
